

function checkVersion() {
  window.api?.checkUpdae(handleUpdateEvent)
}

function startUpdate() {
  window.api?.startUpdate()
}

function stopUpdate() {
  window.api?.stopUpdate()
}

function sendMessage(info) {
  uni.$emit('Evt_Hotupdate', info) 
}

function handleUpdateEvent(data) {
  console.log('=== [hotupdateMgr] handleUpdateEvent, cmd: ', data.cmd) 
  
  if (data.cmd === 'update-not-available') {
    sendMessage({
      state: 'newest', 
    })
  } else if (data.cmd === 'update-available') {
    sendMessage({
      state: 'hasNew', // 'none' | 'hasNew' | 'newest' | 'fail' | 'progress' 
      newVersion: data.info.version,
      forceUpdated: false  
    })
  } else if (data.cmd === 'download-progress') {
    sendMessage({
      state: 'progress', 
      percent: data.info.percent,
    })
    
  } else if (data.cmd === 'update-downloaded') {
    sendMessage({
      state: 'success', 
    })
    
  } else if (data.cmd === 'error') {
    sendMessage({
      state: 'fail', 
    })
  } 
}



export default {
  checkVersion,
  startUpdate,
  stopUpdate,
}

